<?php

namespace Biz\model\bizAd;

use Biz\pockets\modelTrait\BizModelTrait;
use Doraemon\pockets\datebase\BaseDbModel;

class AdWalletLog extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_biz_ad';

    const _CONFIG_SELECT_ = 'db_biz_ad';

    const _DATABASE_ = 'biz_ad';

    const _TABLE_NAME = 'hhz_ad_wallet_log';

    const OP_TYPE_RECHARGE = 1; #充值
    const OP_TYPE_CONSUME = 2; #消费
    const OP_TYPE_REFUND = 3; #退款
    const OP_TYPE_CASHOUT = 4; #提现
    const OP_TYPE_RELEASE_LOCK_AMOUNT = 5; #结算在途金额

    const OP_TYPES = [
        self::OP_TYPE_RECHARGE => '充值',
        self::OP_TYPE_CONSUME => '计划生效',
        self::OP_TYPE_REFUND => '计划返还',
        self::OP_TYPE_CASHOUT => '余额提现',
        self::OP_TYPE_RELEASE_LOCK_AMOUNT => '结算在途金额',
    ];

    const OP_UID_TYPE_USER = 1; #用户
    const OP_UID_TYPE_OP = 2; #运营

    const OP_UID_TYPES = [
        self::OP_UID_TYPE_USER => '用户',
        self::OP_UID_TYPE_OP => '运营',
    ];

    const REMARKS = [
        self::OP_TYPE_RECHARGE => '充值',
        self::OP_TYPE_CONSUME => '计划生效',
        self::OP_TYPE_REFUND => '计划返还',
        self::OP_TYPE_CASHOUT => '提现',
        self::OP_TYPE_RELEASE_LOCK_AMOUNT => '在途金额结算',
    ];

    const SYMBOLS = [
        self::OP_TYPE_RECHARGE => '+',
        self::OP_TYPE_CONSUME => '-',
        self::OP_TYPE_REFUND => '+',
        self::OP_TYPE_CASHOUT => '-',
    ];

    use BizModelTrait;

    /**
     * 添加记录
     * @param $walletId
     * @param $uid
     * @param $utype
     * @param $opType
     * @param $oldBalance
     * @param $balance
     * @param $opUidType
     * @param $opUid
     * @param $opRelId
     * @param $remark
     * @param $version //操作后的版本号
     * @return false|int
     */
    public static function addLog(
        $walletId,
        $uid,
        $utype,
        $opType,
        $oldBalance,
        $amount,
        $balance,
        $opUidType,
        $opUid,
        $opRelId,
        $remark,
        $lockAmount,
        $oldLockAmount,
        $version
    ) {
        $data = [
            'uid' => $uid,
            'utype' => $utype,
            'wallet_id' => $walletId,
            'op_type' => $opType,
            'old_balance' => $oldBalance,
            'amount' => $amount,
            'balance' => $balance,
            'op_uid_type' => $opUidType,
            'op_uid' => $opUid,
            'op_rel_id' => $opRelId,
            'remark' => $remark,
            'create_time' => date('Y-m-d H:i:s', time()),
            'version' => $version,
            'lock_amount' => $lockAmount,
            'old_lock_amount' => $oldLockAmount,
        ];

        return self::dataInsert($data);
    }

    /**
     * 是否已操作
     * @param $uid
     * @param $opType
     * @param $opRelId
     * @return bool
     */
    public static function hasOpLog($uid, $opType, $opRelId)
    {
        return self::dataHas(['uid' => $uid, 'op_type' => $opType, 'op_rel_id' => $opRelId]);
    }

    /**
     * 获取备注
     * @param $text
     * @param $opType
     * @return string
     */
    public static function getRemark($opType, $text = '')
    {
        $remark = '';
        switch ($opType) {
            case self::OP_TYPE_RECHARGE:
            case self::OP_TYPE_CASHOUT:
                $remark = self::REMARKS[$opType];
                break;
            case self::OP_TYPE_CONSUME:
            case self::OP_TYPE_REFUND:
            case self::OP_TYPE_RELEASE_LOCK_AMOUNT:
                $remark = $text . self::REMARKS[$opType];
                break;
        }
        return $remark;
    }

}